package logic

import (
	"context"
	"encoding/json"
	"github.com/zeromicro/go-zero/core/logx"
	"wsyxapi/client"
	"wsyxapi/internal/svc"
	"wsyxapi/internal/types"

	"github.com/2103A/message/order"
)

type WsyxapiLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewWsyxapiLogic(ctx context.Context, svcCtx *svc.ServiceContext) *WsyxapiLogic {
	return &WsyxapiLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *WsyxapiLogic) Wsyxapi(req *types.CreateOrderRequest) (resp *types.CreateOrderResponse, err error) {

	// todo: add your logic here and delete this line
	token := l.ctx.Value("id")
	valinfo := token.(json.Number)
	userID, err := valinfo.Int64()
	if err != nil {
		return nil, err
	}
	//TODO:检查用户信息
	//TODO:检查商品库存
	//TODO:计算商品金额

	infos := []*order.PurchaseGoods{}
	for _, val := range req.Infos {
		infos = append(infos, &order.PurchaseGoods{
			GoodsId: int32(val.GoodSKUID),
			Number:  int32(val.Num),
		})
	}
	url, err := client.OrderCreate(context.Background(), userID, infos)
	if err != nil {
		return nil, err
	}
	return &types.CreateOrderResponse{Url: url}, nil
}
